﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Validation;
using Entities;
using Utilities;
using DataAccessLayer;
using System.Linq.Expressions;

namespace BusinessLogicLayer
{
    public class LopHocManager :IValidation<LopHoc>
    {
        private readonly IRepository<LopHoc> _repository;

        public LopHocManager(IRepository<LopHoc> itr)
        {
            _repository = itr;
        }

        public ValidationResult Validate(LopHoc obj)
        {
            var result = new ValidationResult();
            if (obj.SiSo <= 0)
                result.Messages.Add("Si so phai lon hon 0");
            return result;
        }

        public ActionResult InsertOrUpdate(LopHoc lh)
        {
            var result = new ActionResult();
            try
            {
                if (!_repository.InsertOrUpdate(lh))
                    result.ex.Add(new Exception("Them lop hoc khong thanh cong"));
            }
            catch (Exception ex){
                result.ex.Add(ex);
            }
            return result;
        }
        public ActionResult Delete(string lophocId)
        {
            var aResult = new ActionResult();
            try
            {
                if (!_repository.Delete(t => t.MaLop == lophocId))
                    aResult.ex.Add(new Exception("Xóa thông tin lớp học không thành công"));
            }
            catch (Exception ex)
            {
                aResult.ex.Add(ex);
            }
            return aResult;
        }

        public ActionResultAsList<LopHoc> Find(Expression<Func<LopHoc, bool>> predicate)
        {
            var aResult = new ActionResultAsList<LopHoc>();
            try
            {
                aResult.Result = _repository.Find(predicate).ToList();
                if (aResult.Result.Count == 0)
                    aResult.ex.Add(new Exception("Không tìm thấy lớp học thỏa điều kiện"));
            }
            catch (Exception ex)
            {
                aResult.ex.Add(ex);
            }
            return aResult;
        }
    }
}
